package cn.lcc.utils.utils.method;

import cn.lcc.utils.commom.annotation.Endpoint;
import cn.lcc.utils.commom.annotation.Mapping;
import cn.lcc.utils.commom.pack.UD_DEMO;
import cn.lcc.utils.utils.ClassUtils;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 解析
 */
public class QuickStart {
    private final static Map<Object, Method> handlerMap = new HashMap<>(64);

    public static String UD_DEMO = "33472a393632303435393434302a303039312a44454d4f2c3031303732342c3031303530382c412c33322e3933383336332c4e2c3131352e383639373638332c452c302e33352c3230322e332c302e302c31322c3130302c35342c302c302c30303030303030302c312c302c3436302c302c32323234392c3235323236353535322c3138382c312c2c39433a37463a38313a37333a41353a38422c2d39332c302e30";


    public static void main(String[] args) {
        HandlerMapping("cn.lcc.utils.utils.method");
        handlerMap.forEach((k,v)->{
            try {
                v.invoke(null);
            } catch (IllegalAccessException e) {
                throw new RuntimeException(e);
            } catch (InvocationTargetException e) {
                throw new RuntimeException(e);
            }
        });
    }

    public static void HandlerMapping(String endpointPackage) {
        List<Class> endpointClasses = ClassUtils.getClassList(endpointPackage, Endpoint.class);
        for (Class endpointClass : endpointClasses) {
            try {
                Object bean = endpointClass.getDeclaredConstructor((Class[]) null).newInstance((Object[]) null);
                registerHandlers(bean);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    }

    /**
     * 将Endpoint中被@Mapping标记的方法注册到映射表
     */
    public static synchronized void registerHandlers(Object bean) {
        Class<?> beanClass = bean.getClass();
        Method[] methods = beanClass.getDeclaredMethods();

        for (Method method : methods) {
            Mapping mapping = method.getAnnotation(Mapping.class);
            try {
                Object[] objects = new Object[method.getParameterTypes().length];
                UD_DEMO d = new UD_DEMO();
                d.setName("asdasd");
                objects[0] = d;
                if (method.getName().equals("UD_DEMO")){
                    method.invoke(bean, objects);
                }
            } catch (IllegalAccessException e) {
                throw new RuntimeException(e);
            } catch (InvocationTargetException e) {
                throw new RuntimeException(e);
            }

            if (mapping != null) {
                String desc = mapping.desc();
                int[] types = mapping.types();
//                handlerMap.put(types[0], method);
//                Handler handler;
//                handler = new SimpleHandler(bean, method, desc, true);
                for (int type : types) {
                    handlerMap.put(type, method);
                }
            }
        }
    }

}
